<?php

namespace App\Http\Service;

use App\Http\Model\AreaModel;
use App\Http\Model\RegionModel;
use App\Http\Model\TempAreaModel;
use App\Http\Service\CommonService;
use Illuminate\Support\Facades\DB;

class AreaService {

    public static function area() {
        //一步一步的执行
//        self::step1();
//        self::step2();
        self::step3();
    }

    public static function step1() {
        DB::beginTransaction();
        try {
            $list = TempAreaModel::get()->toArray();
            foreach ($list as $k => $v) {
                $ms = RegionModel::where('region_code', $v['code'])->get();
                if ($ms->isEmpty()) {
                    //第一级的时候，直接插入
                    if (mb_substr($v['code'], 2, 4) == '0000') {
                        echo '第一级';
                        $RegionModel1 = new RegionModel();
                        $RegionModel1->region_code = $v['code'];
                        $RegionModel1->parent_region_code = 100000;
                        $RegionModel1->region_name = $v['name'];
                        $RegionModel1->level = 1;
                        $RegionModel1->save();
                    }
                } else {
                    CommonService::p($v['code'] . '有数据');
                }
            }
            DB::commit();
            return $list;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public static function step2() {
        DB::beginTransaction();
        try {
            $list = TempAreaModel::get()->toArray();
            foreach ($list as $k => $v) {
                $ms = RegionModel::where('region_code', $v['code'])->get();

                if ($ms->isEmpty()) {
                    if (mb_substr($v['code'], 4, 2) == '00') {

                        echo '第二级';
                        //第二级的时候，查询父级
                        $region_code = mb_substr($v['code'], 0, 2) . '0000';
                        $rm2 = RegionModel::where('region_code', $region_code)->get();
                        if ($rm2->isEmpty()) {
                            throw new \Exception('数据丢失');
                        }
                        $RegionModel2 = new RegionModel();
                        $RegionModel2->region_name = $v['name'];
                        $RegionModel2->region_code = $v['code'];
                        $RegionModel2->parent_region_code = $region_code;
                        $RegionModel2->level = 2;
                        $RegionModel2->save();
                    }
                } else {
                    CommonService::p($v['code'] . '已存在');
                }
            }
            DB::commit();
            return $list;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public static function step3() {
        DB::beginTransaction();
        try {
            $list = TempAreaModel::get()->toArray();
            foreach ($list as $k => $v) {
                $ms = RegionModel::where('region_code', $v['code'])->get();
                if ($ms->isEmpty()) {
                    echo '第三级';
                    //有两种情况，一种是直辖市下面的县/区 一种是省->市->普通市的县/区
                    $region_code = mb_substr($v['code'], 0, 4) . '00';
                    CommonService::p($region_code);
                    $rm3 = RegionModel::where('region_code', $region_code)->get();
                    if ($rm3->isEmpty()) {
                        echo $region_code . "父3 region_code没有查到,是直辖市，添加直辖市城区";
                        $region_code1 = mb_substr($v['code'], 0, 2) . '0000';
                        $rm2 = RegionModel::where('region_code', $region_code1)->get();
                        if ($rm2->isEmpty()) {
                            throw new \Exception($region_code . "父1一级没有查到");
                        }
                        $RegionModel4 = new RegionModel();
                        //第三级的时候，查询父级
                        $RegionModel4->region_code = $region_code;
                        $RegionModel4->parent_region_code = $region_code1;
                        $RegionModel4->region_name = $rm2['0']->region_name . '城区';
                        $RegionModel4->level = 2;
                        $RegionModel4->save();
                    }
                    $RegionModel3 = new RegionModel();
                    //第三级的时候，查询父级
                    $RegionModel3->region_code = $v['code'];
                    $RegionModel3->parent_region_code = $region_code;
                    $RegionModel3->region_name = $v['name'];
                    $RegionModel3->level = 3;
                    $RegionModel3->save();
                } else {
                    CommonService::p($v['code'] . '有数据');
                }
            }
            DB::commit();
            return $list;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }

    public static function region() {
        
    }

}
